package com.yndsoft.jc.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2016-08-17.
 * 调用系统拍照或进入图库中选择照片,再进行裁剪,压缩.
 */
public class ImgUtils {
    //加载并显示一副图像对内存使用情况有显著的影响，Android提供了一个名为BitmapFactory 的实用程序类，该程序提供了一系列的静态方法，允许通过各种来源加载Bitmap图像。针对我们的需求，将从文件加载图像，并在最初的活动中显示它。幸运的是，BitmapFactory中的可用方法将会调用BitmapFactory.Options类，这使得我们能够定义如何将Bitmap读入内存。具体而言，当加载图像时，可以设置BitmapFactory应该使用的采样大小。在BitmapFactory.Options中指定inSampleSize参数。例如，将inSampleSize
    //= 8时，产生一幅图的大小是原始大小的1/8。要注意的是首先应将BitmapFactoryOptions.inJustDecodeBounds变量设置为true,这将通知BitmapFactory类只需返回该图像的范围，而无需尝试解码图像本身。最后将BitmapFactory.Options.inJustDecodeBounds设置为false，最后对其进行真正的解码。

    /**
     * @param picPath
     * @param reqWidth
     * @param reqHeight
     * @param compress
     * @return
     */
    public static Bitmap getSmallBitmap(String picPath, int reqWidth, int reqHeight, int compress) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(picPath, options);  //options中将获得图片一些信息
        options.inSampleSize = calulateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(picPath, options);
        if (bitmap == null) {
            return null;
        }
        int degree = readPictureDegree(picPath);
        bitmap = rotateBitmap(bitmap, degree);
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            //压缩图片质量
            bitmap.compress(Bitmap.CompressFormat.JPEG, compress, baos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    private static int calulateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        //Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
        }
        return inSampleSize;
    }

    /**
     * 读取图片旋转处理
     *
     * @param path
     * @return
     */
    private static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                default:
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * 图片旋转处理
     *
     * @param bitmap
     * @param rotate
     * @return
     */
    private static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
        if (bitmap == null) {
            return null;
        }
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }
}
